package com.example.dbmeta.exception;
import java.text.MessageFormat;
import java.time.Instant;

public class DbMetaBootException extends RuntimeException {
	private final DbMetaErrorCode errorCode;
	private final Instant timestamp;
	private final String errorDetails;
	private final String configPath;
	private final String suggestedFix;

	public DbMetaBootException(DbMetaErrorCode errorCode, Object... params) {
		this(errorCode, null, null, null, params);
	}

	public DbMetaBootException(DbMetaErrorCode errorCode, String configPath, Object... params) {
		this(errorCode, configPath, null, null, params);
	}

	public DbMetaBootException(DbMetaErrorCode errorCode, String configPath,
							   String errorDetails, Object... params) {
		this(errorCode, configPath, errorDetails, null, params);
	}

	public DbMetaBootException(DbMetaErrorCode errorCode, String configPath,
							   String errorDetails, String suggestedFix, Object... params) {
		super(errorCode.getMessage(params));
		this.errorCode = errorCode;
		this.timestamp = Instant.now();
		this.configPath = configPath;
		this.errorDetails = errorDetails;
		this.suggestedFix = suggestedFix != null ? suggestedFix : generateDefaultFix();
	}

	public int getErrorCode() {
		return errorCode.getCode();
	}

	public DbMetaErrorCode getErrorCodeEnum() {
		return errorCode;
	}

	public Instant getTimestamp() {
		return timestamp;
	}

	public String getErrorDetails() {
		return errorDetails;
	}

	public String getConfigPath() {
		return configPath;
	}

	public String getSuggestedFix() {
		return suggestedFix;
	}

	public String getFormattedErrorReport() {
		StringBuilder report = new StringBuilder();
		report.append("\n================= DB Meta 启动错误 =================");
		report.append("\n错误代码: ").append(errorCode.getCode());
		report.append("\n错误消息: ").append(getMessage());
		report.append("\n发生时间: ").append(timestamp);

		if (configPath != null) {
			report.append("\n配置文件: ").append(configPath);
		}

		if (errorDetails != null) {
			report.append("\n错误详情:\n").append(errorDetails);
		}

		report.append("\n\n建议修复方案:");
		report.append("\n  → ").append(suggestedFix);
		report.append("\n\n更多帮助: https://docs.your-app.com/errors/").append(errorCode.getCode());
		report.append("\n==================================================");

		return report.toString();
	}

	private String generateDefaultFix() {
		switch (errorCode) {
			case DUPLICATE_DATA_INSTANCE:
				return "为每个数据源实例指定唯一的名称。修改配置: " +
						"\n    <datainstance>" +
						"\n        <dbinstancename>唯一名称</dbinstancename>" +
						"\n        ..." +
						"\n    </datainstance>";

			case INVALID_KEEPDAYS:
				return "调整 keepdays 值至 1-365 之间: " +
						"\n    <keepdays>30</keepdays>";

			case MISSING_DEST_DATASOURCE:
				return "添加 destinationdatasource 配置块: " +
						"\n    <destinationdatasource>" +
						"\n        <url>jdbc:mysql://localhost:3306/db</url>" +
						"\n        ..." +
						"\n    </destinationdatasource>";

			default:
				return "检查配置文件的语法和完整性。";
		}
	}
}
//
///**
// * @Description: jeecg-boot自定义异常
// * @author: jeecg-boot
// */
//public class DbMetaBootException extends RuntimeException {
//	private static final long serialVersionUID = 1L;
//
//	/**
//	 * 返回给前端的错误code
//	 */
//	private int errCode = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
//
//	public DbMetaBootException(String message){
//		super(message);
//	}
//
//	public DbMetaBootException(String message, int errCode){
//		super(message);
//		this.errCode = errCode;
//	}
//
//	public int getErrCode() {
//		return errCode;
//	}
//
//	public DbMetaBootException(Throwable cause)
//	{
//		super(cause);
//	}
//
//	public DbMetaBootException(String message, Throwable cause)
//	{
//		super(message,cause);
//	}
//}
